/*
 * Copyright (c) 2011 Sapienza University of Rome.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the
 *   distribution.
 * - Neither the name of the Sapienza University of Rome nor the names of
 *   its contributors may be used to endorse or promote products derived
 *   from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL SAPIENZA
 * UNIVERSITY OF ROME OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/**
 * @author Ugo Colesanti <colesanti@dis.uniroma1.it>
 * @version 1.01 (October 10, 2011)
 *
 */

#ifndef NTP_MANAGER_H
#define NTP_MANAGER_H

enum{
	AM_SINKGATEWAYMSG = 0x99, // message from sink to gateway (with scheduleInfo)
	AM_HEARTBEATMSG = 0x96,	  // heart beat message from sink to gateway

	HT_MAX_COUNT = 5, // max count before out of sync between sink and gateway


	STATE_REQUEST = 0 ,
	STATE_REPLY = 1 ,
	CHANGE_STATE_TO_SINK = 2 ,
	CHANGE_STATE_TO_NODE = 3 ,
	CHANGE_STATE_ACK = 4 ,
	NTP_ADJUST_REQ = 5,
	NTP_ADJUST_ACK = 6,
	SCHEDULE_UPDATE_REQ = 7,
	SCHEDULE_UPDATE_ACK = 8,

	RESET_NODE = 10,

	HEART_BEAT_REQ = 20,
	HEART_BEAT_ACK = 21,
};

// SinkGatewayMessage is used to exchange information between the Sink and the GUI.
typedef nx_struct sinkGatewayMsg{
	nx_uint8_t	type ; // to be defined
	nx_uint8_t state ; // bit n.1 is 0 if not sink, 1 if sink | bit n.2 is 0 if not ack, 1 if ack | bit n.3 is 1 if resync
	nx_uint16_t current_sampling_period ;		// 2
	nx_uint16_t current_schedule_gt ;			// 2
	nx_uint16_t current_schedule_ri ;			// 2
	nx_uint16_t current_schedule_dci ;			// 2
	nx_uint8_t current_schedule_skip ;			// 1
	nx_uint32_t current_timestamp ;				// 4
	nx_uint16_t next_sampling_period ;			// 2
	nx_uint32_t time_to_next_sampling_period ;  // 4
	nx_uint32_t next_sampling_period_fired ;	// 4
}sinkGatewayMsg_t ;


// Heart beat message. It is sent periodically by both, GUI and NtpManager to keep connection active between them.
typedef nx_struct heartBeatMsg{
	nx_uint8_t	type ; // hearbeat
}heartBeatMsg_t ;

#endif
